
********************************************************************************************************************************
* This file replicates the tables and figures from Eren and Mocan, "Juvenile Punishment, High School Graduation and Adult Crime:
* "Evidence from Idiosyncratic Harshness", Review of  Economics and Statistics
* Additional information is provided in the readme file.
******************************************************************************************************************************** 

set matsize 5000
clear all
set more off
global data " "
global dir " "

******use estimation_sample, estimation_sample_non_s and education_sample to produce the results reported throughout the paper.

use $data/estimation_sample, clear

global juvenile "black other female juvenile_age_year juvenile_age_square_year"
global crime_type "i.adj_statute1"
global crime_type_juv "drug_juvenile violent_juvenile property_juvenile"


*****Table 1
summ ojj_secure black white female juvenile_age_year 
summ drug_juvenile violent_juvenile property_juvenile  other_juvenile if adj_statute1!="999"
gen offense_age=offense_year-juvenile_birth_year 
sum offense_age if offense_age>14 & crime==1
sum crime drug_tek violent_tek property_tek 
preserve
keep if ojj_secure==1
summ ojj_secure black white female juvenile_age_year 
summ drug_juvenile violent_juvenile property_juvenile  other_juvenile if adj_statute1!="999"
sum offense_age if crime==1 & offense_age>14
sum crime drug_tek violent_tek property_tek other_crime_tek 
restore
keep if ojj_secure==0
summ ojj_secure black white female juvenile_age_year 
summ drug_juvenile violent_juvenile property_juvenile  other_juvenile if adj_statute1!="999"
sum offense_age if offense_age>14 & crime==1
sum crime drug_tek violent_tek property_tek 


*****Figures 1 and B1
hist peer_effect_secure, freq bin(100) ylabel (, nogrid) xlabel(, nogrid) xtitle("Judge Stringency-Raw Variation") ytitle("Frequency") plotregion(ifcolor(white)) bcolor(gs14) blcolor(gs8) ///
lwidth(vvvthin) lpattern(solid) fintensity(full) graphregion(fcolor(white)) graphregion(ilcolor(white)) plotregion(ilcolor(white)) 

xi:regress peer_effect_secure $juvenile $crime_type i.court_year
predict e_hat, residuals
summ peer_effect_secure
replace e_hat=e_hat+r(mean)
hist e_hat, freq bin(100) ylabel (, nogrid) xlabel(, nogrid) xtitle("Judge Stringency") ytitle("Frequency") plotregion(ifcolor(white)) bcolor(gs14) blcolor(gs8) ///
lwidth(vvvthin) lpattern(solid) fintensity(full) graphregion(fcolor(white)) graphregion(ilcolor(white)) plotregion(ilcolor(white)) 

*********Table 3
foreach var in black white female juvenile_age_year {
xi:ivreg2 peer_effect_secure `var' i.court_year, cluster(disp_judge) 
}

foreach var in drug_juvenile violent_juvenile property_juvenile felony {
xi:ivreg2 peer_effect_secure `var' i.court_year if adj_statute11!="" , cluster(disp_judge) 
}

xi: ivreg2 ojj_secure $joint  i.court_year if adj_statute11!="", cluster(disp_judge)
test $joint

******Table 4
use $data/estimation_sample, clear

gen time_in_hundred=time_in_prison/100
gen sentence_length=fulltermdate-dispdate

xi:ivreg2 time_in_hundred  peer_effect_secure $juvenile $crime_type i.court_year, cluster(disp_judge)
xi:ivreg2 time_in_hundred  peer_effect_secure $juvenile $crime_type i.court_year if ojj_secure==1, cluster(disp_judge)
xi:ivreg2 time_in_hundred ojj_secure peer_effect_secure $juvenile $crime_type i.court_year, cluster(disp_judge)


******Table 5
foreach var in crime drug_tek violent_tek property_tek {

xi:ivreg2 `var' ojj_secure  i.court_year, cluster(disp_judge)
xi:ivreg2 `var' ojj_secure $juvenile  i.court_year, cluster(disp_judge)
xi:ivreg2 `var' ojj_secure $juvenile $crime_type i.court_year, cluster(disp_judge)
}

******Table 5-Complier Weights
use $data/estimation_sample, clear

_pctile peer_effect_secure, nq(100)
return list r99 r1
global p1=r(r1)
global p99=r(r99)
xi: regress ojj_secure $juvenile $crime_type_juv i.court_year
predict p_risk, xb
_pctile p_risk, nq(4)
gen p1=p_risk<=r(r1)
gen p2=p_risk>r(r1) & p_risk<=r(r2)
gen p3=p_risk>r(r2) & p_risk<=r(r3)
gen p4=p_risk>r(r3)  & p_risk<=r(r4)
gen complier_weight=.

foreach var in p1 p2 p3 p4 {
regress ojj_secure peer_effect_secure i.court_year if `var'==1
gen frac_complier_`var'=(_b[_cons] + _b[peer_effect_secure]*$p99) - (_b[_cons] + _b[peer_effect_secure]*$p1) if `var'==1
egen frac_`var' = mean(`var') 
replace complier_weight=frac_complier_`var'/frac_`var' if `var'==1 & complier_weight==.
}
keep ref_number complier_weight
tempfile comp_weights
save "`comp_weights'"

use $data/estimation_sample, clear

capture drop _merge
merge 1:1 ref_number using "`comp_weights'"

foreach var in crime drug_tek violent_tek property_tek {
xi:ivreg2 `var' ojj_secure $juvenile $crime_type i.court_year [w=complier_weight], cluster(disp_judge)
}


******Tables 2 and 6
use $data/estimation_sample, clear

foreach var in crime drug_tek violent_tek property_tek  {
xi:ivreg2 `var' i.court_year (ojj_secure=peer_effect_secure), first cluster(disp_judge)
xi:ivreg2 `var' $juvenile i.court_year (ojj_secure=peer_effect_secure), first cluster(disp_judge)
xi:ivreg2 `var' $juvenile $crime_type i.court_year (ojj_secure=peer_effect_secure), first cluster(disp_judge)
}


foreach var in crime drug_tek violent_tek property_tek  {
xi:ivreg2 `var' peer_effect_secure $juvenile $crime_type i.court_year , cluster(disp_judge)
}

*****Table 7
use $data/estimation_sample, clear

centile time_in_prison if ojj_secure==1, centile(50)
global med_pri=r(c_1)
gen medium_prison=time_in_prison>$med_pri
gen time_in_hundred=time_in_prison/100

foreach var in drug_tek violent_tek property_tek  {
xi:ivreg2 `var' $juvenile $crime_type i.court_year (ojj_secure=peer_effect_secure) if ojj_secure==0 | medium_prison==0, cluster(disp_judge)
xi:ivreg2 `var' $juvenile $crime_type i.court_year (ojj_secure=peer_effect_secure) if ojj_secure==0 | medium_prison==1, cluster(disp_judge)
}

*****Table 8
use $data/education_sample, clear

xi:ivreg2 high_graduate ojj_secure $juvenile  i.court_year, cluster(disp_judge)
xi:ivreg2 high_graduate ojj_secure $juvenile $crime_type i.court_year, cluster(disp_judge)

xi:ivreg2 high_graduate $juvenile i.court_year (ojj_secure=peer_effect_secure), first cluster(disp_judge)
xi:ivreg2 high_graduate $juvenile $crime_type i.court_year (ojj_secure=peer_effect_secure), first cluster(disp_judge)

gen early=juvenile_birth_year<=1982


foreach var in early {
gen `var'_ojj_secure=`var'*ojj_secure
gen `var'_peer_effect_secure=`var'*peer_effect_secure
}


xi:ivreg2 high_graduate ojj_secure early_ojj_secure early $juvenile $crime_type i.court_year, first cluster(disp_judge)
xi:ivreg2 high_graduate early $juvenile $crime_type i.court_year (ojj_secure early_ojj_secure=peer_effect_secure early_peer_effect_secure), first cluster(disp_judge)

*****Figure 2
use $data/education_sample, clear

collapse high_graduate, by(juvenile_birth_year ojj_secure)
replace high_graduate=high_graduate*100
keep if juvenile_birth_year>=1980

twoway (line high_graduate juvenile_birth_year if ojj_secure==1) (line high_graduate juvenile_birth_year if ojj_secure==0, lcolor(red) lpattern(longdash)), ///
ytitle("High School Graduation Rates(%)") xtitle("Birth Cohorts") ylabel(0(20)100, angle(horizontal) format(%9.0gc) tposition(outside) nogrid) xline(1982, lcolor(black) lpattern(dash)) ///
xtitle("") xlabel(, nogrid) graphregion(fcolor(white) lcolor(none)) ///
legend(order(1 "Incarcerated" 2 "Non-Incarcerated")) legend(pos(1) ring(0) col(1)) legend(region(lcolor(white))) 

*******Tables B1 
use $data/estimation_sample, clear

foreach var_c in black white female juvenile_age_year {
xi:ivreg2 ojj_secure `var_c'  i.court_year, cluster(disp_judge)
}

foreach var_c in  drug_juvenile violent_juvenile property_juvenile felony  {
xi:ivreg2 ojj_secure `var_c'  i.court_year if adj_statute11!="" , cluster(disp_judge)
}

global joint "black white female juvenile_age_year drug_juvenile violent_juvenile property_juvenile felony"
xi: ivreg2 peer_effect_secure $joint  i.court_year if adj_statute11!="", cluster(disp_judge)
test $joint

******Tables B2 & B3
use $data/estimation_sample, clear

gen age_lte_15=juvenile_age_year<=15
gen age_mt_15=juvenile_age_year>15

foreach var in black white age_lte_15 age_mt_15 {

xi: ivreg2 ojj_secure  peer_effect_secure $juvenile $crime_type i.court_year if `var'==1, cluster(disp_judge)

}

use $data/estimation_sample, clear

foreach var in black white age_lte_15 age_mt_15 {

use  $data/judge_estimation, clear
gen age_lte_15=juvenile_age_year<=15
gen age_mt_15=juvenile_age_year>15
drop if adj_statute11==""
drop if `var'==1
duplicates tag ref_number, gen(ref_number_dup)
drop if ref_number_dup>0
gen year=year(dispdate)
gen ojj_secure=l_status=="Custody Secure" 

gen peer_effect_secure=.
sort disp_judge, stable
egen mean=mean(ojj_secure), by(disp_judge)
replace peer_effect_secure=mean
drop if peer_effect_secure==.
keep disp_judge year court peer_effect_secure
collapse (first) peer_effect_secure, by(disp_judge)
tempfile `var'_juv_no

save "``var'_juv_no'"

}

use $data/estimation_sample, clear

foreach var in black white age_lte_15 age_mt_15 {
preserve
gen age_lte_15=juvenile_age_year<=15
gen age_mt_15=juvenile_age_year>15
capture drop _merge
keep if `var'==1
drop peer_effect_secure
merge m:1 disp_judge using "``var'_juv_no'"
keep if _merge==3
xi: ivreg2 ojj_secure  peer_effect_secure $juvenile $crime_type i.court_year, cluster(disp_judge)
restore
}

use $data/estimation_sample, clear

drop if adj_statute1=="999"

foreach var in drug_juvenile violent_juvenile property_juvenile other_juvenile {
xi: ivreg2 ojj_secure  peer_effect_secure $juvenile $crime_type i.court_year if `var'==1, cluster(disp_judge)
}


foreach var in drug_juvenile violent_juvenile property_juvenile other_juvenile{

use  $data/judge_estimation, clear

foreach name in DISTRIBUTION DRUG CDS MARIJUANA SUBST {
gen `name'=regexm(adj_statute1,"`name'")
}

gen drug_juvenile=DRUG==1 | CDS==1 | MARIJUANA==1
replace drug_juvenile=1 if adj_statute1=="FALSE REPRESNT CONT SUBST"


foreach name in ASSAULT  RAPE ARMED CARJACKING DISARM EXTOR SLAUGH MURDER  PURSE KIDNAP ROBBERY STALK {
gen `name'=regexm(adj_statute1,"`name'")
}

gen violent_juvenile=ASSAULT==1 |  RAPE==1 | ARMED==1 | CARJACKING==1 | DISARM==1 | EXTOR==1 | SLAUGH==1 | MURDER==1 |  PURSE==1 | KIDNAP==1 | ROBBERY==1 | STALK==1


replace violent_juvenile=1 if adj_statute1=="AGG BATTERY"
replace violent_juvenile=1 if adj_statute1=="AGG BATTERY 2ND DEGREE"
replace violent_juvenile=1 if adj_statute1=="AGG BURGLARY"
replace violent_juvenile=1 if adj_statute1=="AGG CRIM DAMAGE-PROP"
replace violent_juvenile=1 if adj_statute1=="AGG ARSON"



foreach name in FRAUD MISCHIEF FORGERY  STOL DWELL THEFT  ENTRY MOVABLE {
gen `name'=regexm(adj_statute1,"`name'")
}

gen property_juvenile=FRAUD==1 | MISCHIEF==1 | FORGERY==1 | STOL==1 | DWELL==1 | THEFT==1 | ENTRY==1 | MOVABLE==1  

replace property_juvenile=1 if adj_statute1=="DAM-COIN DEV-<$100"
replace property_juvenile=1 if adj_statute1=="SCD-PROP $50000"
replace property_juvenile=1 if adj_statute1=="SCD-PROP 500-49999"
replace property_juvenile=1 if adj_statute1=="SMP CRIM DAM PROP <500"
replace property_juvenile=1 if adj_statute1=="TRANS AND POSS OF STOLEN VEH"
replace property_juvenile=1 if adj_statute1=="UN USE CR CD $500+"
replace property_juvenile=1 if adj_statute1=="UN USE CRED CD <$100"
replace property_juvenile=1 if adj_statute1=="UN USE OF MOTOR VEH"
replace property_juvenile=1 if adj_statute1=="SIMPLE ARSON"
replace property_juvenile=1 if adj_statute1=="SIMPLE BURGLARY"



gen other_juvenile=1 if drug_juvenile==0 & violent_juvenile==0 & property_juvenile==0
replace other_juvenile=0 if other_juvenile==.

drop if adj_statute11==""
drop if `var'==1
duplicates tag ref_number, gen(ref_number_dup)
drop if ref_number_dup>0
gen year=year(dispdate)
gen ojj_secure=l_status=="Custody Secure" 

gen peer_effect_secure=.
sort disp_judge, stable
egen mean=mean(ojj_secure), by(disp_judge)
replace peer_effect_secure=mean
drop if peer_effect_secure==.
keep disp_judge year court peer_effect_secure
collapse (first) peer_effect_secure, by(disp_judge)
tempfile `var'_juv_no

save "``var'_juv_no'"

}

use $data/estimation_sample, clear

drop if adj_statute1=="999"

foreach var in drug_juvenile violent_juvenile property_juvenile other_juvenile {
preserve
capture drop _merge
keep if `var'==1
drop peer_effect_secure
merge m:1 disp_judge using "``var'_juv_no'"
keep if _merge==3
xi: ivreg2 ojj_secure  peer_effect_secure $juvenile $crime_type i.court_year, cluster(disp_judge)
restore
}

****Table B4
use $data/estimation_sample, clear

sort ref_number, stable
_pctile peer_effect_secure, nq(100)
return list r99 r1
global p1=r(r1)
global p99=r(r99)
xi:regress ojj_secure peer_effect_secure  i.court_year
global frac_complier=(_b[_cons] + _b[peer_effect_secure]*$p99) - (_b[_cons] + _b[peer_effect_secure]*$p1)
global frac_always=_b[_cons]+_b[peer_effect_secure]*$p1
global frac_never=1-_b[_cons] - _b[peer_effect_secure]*$p99

di $frac_complier 
di $frac_always 
di $frac_never

gen male=female==0
gen nonwhite=white==0
gen young=juvenile_age_year<=15
gen old=juvenile_age_year>15
gen no_felony=felony==0

gen complier_weight1=.

foreach var in male female {
regress ojj_secure peer_effect_secure i.court_year if `var'==1
gen frac_complier_`var'=(_b[_cons] + _b[peer_effect_secure]*$p99) - (_b[_cons] + _b[peer_effect_secure]*$p1) if `var'==1
replace complier_weight1=frac_complier_`var' if frac_complier_`var'!=.
}

sum male female 
sum male female [aweight=complier_weight1]

gen complier_weight2=.

foreach var in white nonwhite {
regress ojj_secure peer_effect_secure i.court_year if `var'==1
gen frac_complier_`var'=(_b[_cons] + _b[peer_effect_secure]*$p99) - (_b[_cons] + _b[peer_effect_secure]*$p1) if `var'==1
replace complier_weight2=frac_complier_`var' if frac_complier_`var'!=.
}

sum white nonwhite 
sum white nonwhite  [aweight=complier_weight2]

gen complier_weight3=.

foreach var in young old {
regress ojj_secure peer_effect_secure i.court_year if `var'==1
gen frac_complier_`var'=(_b[_cons] + _b[peer_effect_secure]*$p99) - (_b[_cons] + _b[peer_effect_secure]*$p1) if `var'==1
replace complier_weight3=frac_complier_`var' if frac_complier_`var'!=.
}

sum young old 
sum young old  [aweight=complier_weight3]

gen complier_weight4=.

foreach var in felony no_felony {
regress ojj_secure peer_effect_secure i.court_year if `var'==1
gen frac_complier_`var'=(_b[_cons] + _b[peer_effect_secure]*$p99) - (_b[_cons] + _b[peer_effect_secure]*$p1) if `var'==1
replace complier_weight4=frac_complier_`var' if frac_complier_`var'!=.
}

sum felony no_felony
sum felony no_felony  [aweight=complier_weight4]

****Table B5
use $data/estimation_sample, clear

gen offense_age=offense_year-juvenile_birth_year 

foreach var in drug_tek violent_tek property_tek {
gen `var'_19=`var' & offense_age>=19
gen `var'_21=`var' & offense_age>=21
}

foreach var in drug_tek_19 violent_tek_19 property_tek_19 drug_tek_21 violent_tek_21 property_tek_21 {
xi:ivreg2 `var' $juvenile $crime_type i.court_year (ojj_secure =peer_effect_secure), first cluster(disp_judge)
}

*****Table C1
*****Column 1
use $data/estimation_sample, clear

foreach var in  drug_25_tek violent_25_tek property_25_tek   {
xi:ivreg2 `var' $juvenile $crime_type i.court_year (ojj_secure=peer_effect_secure), first cluster(disp_judge)
}
*****Various restrictions need to be imposed to produce the results in Columns 2-10 of Table C1.
*****Column 2-number of total court dispositions needs to at least 15-drop if court_dispositions<15 (replace drop if court_dispositions<25).
*****Column 3-number of total court dispositions needs to at least 250-drop if court_dispositions<250 (replace drop if court_dispositions<15).
*****Column 4-the analysis is limited to urban courts only (Caddo, Baton Rouge, Jefferson and Orleans).
*****Columns 5 and 6 recalculates judge stringency in 5 years intervals and this requires limiting the peer effect calculation to 5 year bins.
*****Judge stringency in Column 7 is allowed to vary by severity of juvenile crime and this requires revising the peer effect calculation as follows: egen mean=mean(ojj_secure*include), by(judge_offense_type).
*****Columns 8 and 9 extends the sample to include all first-time offenders.
*****Column 10 adds the following "if statement" in generating the results for Table 6--petdate==adjdate.


****Table C2
use $data/estimation_sample_non_s, clear

foreach var in drug_tek violent_tek property_tek {
xi:ivreg2 `var' peer_effect_secure peer_effect_non_s $juvenile $crime_type i.court_year, first cluster(disp_judge)
}

foreach var in drug_tek violent_tek property_tek {
xi:ivreg2 `var' $juvenile $crime_type i.court_year (ojj_secure ojj_non_secure=peer_effect_secure peer_effect_non_s), first cluster(disp_judge)
}

*****Table C3
use $data/estimation_sample, clear

foreach var in drug_juvenile property_juvenile violent_juvenile other_juvenile {
gen `var'_s=`var'*ojj_secure
gen `var'_p_e=`var'*peer_effect_secure
}

xi:ivreg2 drug_tek $juvenile $crime_type i.court_year (ojj_secure drug_juvenile_s=peer_effect_secure drug_juvenile_p_e), first cluster(disp_judge)
xi:ivreg2 violent_tek $juvenile $crime_type i.court_year (ojj_secure violent_juvenile_s=peer_effect_secure violent_juvenile_p_e), first cluster(disp_judge)
xi:ivreg2 property_tek $juvenile $crime_type i.court_year (ojj_secure property_juvenile_s=peer_effect_secure property_juvenile_p_e), first cluster(disp_judge)


****Table C4
use $data/estimation_sample, clear

centile juvenile_age_year
global median_age=r(c_1)
gen young=juvenile_age_year<=$median_age

foreach var in black young {
gen `var'_ojj_secure=`var'*ojj_secure
gen `var'_peer_effect_secure=`var'*peer_effect_secure
}


foreach var in drug_tek violent_tek property_tek  {
xi:ivreg2 `var' $juvenile $crime_type i.court_year (ojj_secure black_ojj_secure=peer_effect_secure black_peer_effect_secure), first cluster(disp_judge)
}

foreach var in drug_tek violent_tek property_tek {
xi:ivreg2 `var' $juvenile $crime_type i.court_year (ojj_secure young_ojj_secure=peer_effect_secure young_peer_effect_secure), first cluster(disp_judge)
}
